﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.Events;

namespace SimpleTaskSystem
{
    /// <summary>
    /// 一个操作
    /// </summary>
    [System.Serializable]
    public class Operation
    {
        [Tooltip("操作名")]
        public string operationName;
        [Tooltip("操作描述")]
        public string description;
        [Tooltip("是否为自动执行")]
        public bool isAutomatic = false;
        [Tooltip("提示")]
        public Indicate indicate;
        public UnityEvent onReadyAction;
        /// <summary>
        /// 操作需要执行的方法，当操作为自动执行的时候才会用到
        /// </summary>
        public UnityEvent action;

        public UnityEvent onCompleteAction;
        public delegate void OperationHandler(Operation operation);

        public event OperationHandler onReady;
        public event OperationHandler onCompleted;

        /// <summary>
        /// 准备状态，但并未执行
        /// </summary>
        public virtual void Ready()
        {
            onReadyAction.Invoke();
            if (onReady != null)
            {
                onReady(this);
            }
            if (!isAutomatic)
            {
                if (indicate)
                {
                    indicate.TurnOn();
                }
            }
            else
            {
                Excute();
            }
        }
        /// <summary>
        /// 执行操作，一般在自动执行状态下执行
        /// </summary>
        public virtual void Excute()
        {
            if (indicate)
            {
                indicate.TurnOff();
            }
            action.Invoke();
        }
        /// <summary>
        /// 当完成
        /// </summary>
        public virtual void OnCompleted()
        {
            if (indicate)
            {
                indicate.TurnOff();
            }
            onCompleteAction.Invoke();
            if (onCompleted != null)
            {
                onCompleted(this);
            }
        }
    }
}
